import api from '@/api'
import { deepCopy, setPage } from '@/utils/util'

// 学习报告分页
const initPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 1,
    filter: {
        enabled: null,
        employeeIds: null,
        name: null
    }
}

// 组织报告分页
const oInitPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 1,
    filter: {
        enabled: null,
        employeeIds: null,
        name: null
    }
}

// 组织任务分页
const initTasksPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 1,
    filter: {
        enabled: null,
        name: null
    }
}

// state
const state = {
    data: {
        studyReports: [],
        sOrganizationReports: [],
        studyReportTasks: []
    },
    ui: {
        page: deepCopy(initPage),
        oPage: deepCopy(oInitPage),
        oTaskPage: deepCopy(initTasksPage)
    }
}

// getters
const getters = {
    studyReports: state => state.data.studyReports,
    sOrganizationReports: state => state.data.sOrganizationReports,
    studyReportTasks: state => state.data.studyReportTasks,
    studyReportsTotal: state => state.ui.page.total,
    sOrganizationTotal: state => state.ui.oPage.total,
    studyTaskTotal: state => state.ui.oTaskPage.total
}

// actions
const actions = {
    // 学习报告
    getStudyReports({ state, commit }, data) {
        api.getStudyReports({
            method: 'post',
            data: data ? data : state.ui.page,
            success: (res) => {
                const { pages, total } = res
                commit('SETSTUDYREPORTSPAGE', { pages, total })

                // 字段处理
                if (res.content && res.content.length) {
                    for (let item of res.content) {
                        item.user = item.trainee.username
                        item.name = item.trainee.name
                        item.numberId = item.trainee.employeeId
                        item.jobTitle = item.trainee.jobTitle
                        if (item.trainee.organizationTitles) {
                            item.institution = item.trainee.organizationTitles.join('/')
                        }
                        item.taskFinishedPercent = item.businessCountOfAssigned === 0 ? 0 : parseInt((item.businessCountOfFinished / item.businessCountOfAssigned) * 100) + '%'
                        item.businessPassedPercent = item.businessCountOfAssigned === 0 ? 0 : parseInt((item.businessCountOfPassed / item.businessCountOfAssigned) * 100) + '%'

                    }
                }
                commit('SETSTUDYREPORTS', res.content)
            }
        })
    },
    // 组织报告
    getSOrganizationReports({ state, commit }, data) {
        api.getSOrganizationReports({
            loading: true,
            method: 'post',
            data: data ? data : state.ui.oPage,
            success: (res) => {
                const { pages, total } = res
                commit('SETSORGANIZATIONRPAGE', { pages, total })

                if (res.content && res.content.length) {
                    for (let item of res.content) {
                        item.organizationTitle = item.organization.title
                        item.taskCountOfAssigned = item.taskCountOfAssigned ? item.taskCountOfAssigned : 0
                        item.organizationOfPassed = item.taskCountOfAssigned === 0 ? 0 : parseInt((item.taskCountOfFinished / item.taskCountOfAssigned) * 100) + '%'
                    }
                }
                commit('SETSORGANIZATIONREPORTS', res.content)
            }
        })
    },

    // 组织任务
    getSOrganization({ state, commit }, data) {
        api.getSOrganization({
            loading: true,
            method: 'post',
            data: data ? data : state.ui.oTaskPage,
            success: (res) => {
                const { pages, total } = res
                commit('SETSTUDYORGANIZATIONTASKPAGE', { pages, total })

                if (res.content && res.content.length) {
                    for (let item of res.content) {
                        Object.assign(item, item.task)
                        item.plainTime = `${item.startTime} ~ ${item.endTime}`
                    }
                    commit('SETORGANIZATIONTASKS', res.content)
                }
            }
        })
    },

    getStudyReport({ state, commit }, data = {}) {
        api.getStudyReport({
            loading: data.loading === false ? false : true,
            method: 'post',
            data: JSON.stringify(data) != "{}" ? data : state.ui.oTaskPage,
            success: (res) => {
                const { pages, total } = res
                commit('SETSTUDYORGANIZATIONTASKPAGE', { pages, total })

                if (res.content && res.content.length) {
                    for (let item of res.content) {
                        Object.assign(item, item.task)
                        item.plainTime = `${item.startTime} ~ ${item.endTime ? item.endTime : 'N/A'}`
                    }
                    commit('SETORGANIZATIONTASKS', res.content)
                }
            }
        })
    },

    // 清除员工考试报告
    deleteStudyEmployeeExamReport({ dispatch }, params) {
        api.deleteStudyEmployeeExamReport({
            loading: true,
            method: 'post',
            data: params,
            success: () => {
                dispatch('getStudyReports')
            }
        })
    },
}

// mutations
const mutations = {
    // 学习报告
    SETSTUDYREPORTS(state, payload) {
        state.data.studyReports = payload
    },
    // 组织报告
    SETSORGANIZATIONREPORTS(state, payload) {
        state.data.sOrganizationReports = payload
    },
    // 组织报告任务
    SETORGANIZATIONTASKS(state, payload) {
        state.data.studyReportTasks = payload
    },
    // 学习报告page
    SETSTUDYREPORTSPAGE(state, page) {
        page && setPage(state.ui.page, page)
    },
    // 组织报告page
    SETSORGANIZATIONRPAGE(state, page) {
        page && setPage(state.ui.oPage, page)
    },
    // 组织报告任务
    SETSTUDYORGANIZATIONTASKPAGE(state, page) {
        console.log(page)
        page && setPage(state.ui.oTaskPage, page)
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
